package gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

import objects.Ship;
import engine.Game;




public class MainGUI implements ActionListener{

 //****************** CLASS VARS ***************
	private static JFrame mainframe;
	
	private JTabbedPane sidePane;
	
	private static JPanel mainPanel;
	private static JPanel bottomPanel;
	private static JPanel logPanel;
	//private static JPanel sidePanel;
	
	private JPanel tabStatusPanel;
	private JPanel tabHoldPanel;
	private JPanel tabCrewPanel;
	
	private JButton bstatus;
	private JButton bcombat;
	private JButton bcombatattack;
	private JButton bcombatevade;
	private JButton bcombatstatus;
	
	private JTextArea logwindow;
	
	private JScrollPane logscroll;
	
	//find dynamic way of handling this.
	final Dimension LOG_WINDOW_SIZE = new Dimension(700, 500);
	
	private int currentwindow;
	final int ACTIVE_MAIN = 1;
	final int ACTIVE_STATUS = 2;
	final int ACTIVE_COMBAT = 3;

	/* TODO
	 * 
	 * - Constructor to set up the main frames
	 * - Look for better way to handle window size (override methods?)
	 * 
	 * 
	 * 
	 * - Eventually, custom look to UI
	 */
	
	public MainGUI(){
		
	
		
		
	
		
	}//end constructor
	
	
	
	
 //****************** Frame Setup **************************	
	public void drawMainFrame(){
		mainframe = new JFrame("Main Game Window");
		
		mainframe.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});//end windowListener
		
		
		
		
		currentwindow = ACTIVE_MAIN;
		
		mainPanel = new JPanel();
		mainframe.add(mainPanel);
		mainPanel.setLayout(new BorderLayout());
		
		Border lineB = BorderFactory.createLineBorder(Color.BLACK);
		
		
		logPanel = new JPanel();
		logPanel.setPreferredSize(LOG_WINDOW_SIZE);
		mainPanel.add(logPanel, BorderLayout.LINE_START);
		
		
		initTabs();
		sidePane = new JTabbedPane();
		sidePane.setPreferredSize(new Dimension(200,200));
		sidePane.setBorder(lineB);
		sidePane.addTab("Status", tabStatusPanel);
		sidePane.addTab("Hold", tabHoldPanel);
		sidePane.addTab("Crew", tabCrewPanel);
		mainPanel.add(sidePane, BorderLayout.LINE_END);
		
		bottomPanel = new JPanel();
		bottomPanel.setPreferredSize(new Dimension(500, 100));
		
		bottomPanel.setBorder(lineB);
		mainPanel.add(bottomPanel, BorderLayout.PAGE_END);
		
		
		
		drawMain();
		drawStatus();
				
				
		mainframe.pack();
		mainframe.setVisible(true);


		
	}//end drawMainFrame


	
	

	//*************************** UI ELEMENTS ******************
	
	private void initTabs(){
		tabStatusPanel = new JPanel();
		tabStatusPanel.setLayout(new BoxLayout(tabStatusPanel, BoxLayout.PAGE_AXIS));
		
		tabHoldPanel = new JPanel();
		tabHoldPanel.setLayout(new BoxLayout(tabHoldPanel, BoxLayout.PAGE_AXIS));
		
		tabCrewPanel = new JPanel();
		
		
	}//end initTabs
	
	
	
	public void drawBottomMain(){
		bottomPanel.removeAll();
		String bstatuslabel = "Status";
		
		if (currentwindow == ACTIVE_STATUS){
			bstatuslabel = "Back";
		}//
		
		
		bstatus = new JButton(bstatuslabel);
		bstatus.addActionListener(this);
		bottomPanel.add(bstatus);
		
		
		bcombat = new JButton("Combat");
		bcombat.addActionListener(this);
		bottomPanel.add(bcombat);
		
		refresh(bottomPanel);
	}//end drawBottomMain
	
	public void drawBottomCombat(){
		bottomPanel.removeAll();
		currentwindow = ACTIVE_COMBAT;
		
		
		bcombatattack = new JButton("Fire Weapons!");
		bcombatevade = new JButton("Evade");
		bcombatstatus = new JButton("Status Report");		
		
		bcombatattack.addActionListener(this);
		bcombatevade.addActionListener(this);
		bcombatstatus.addActionListener(this);
		
		
		bottomPanel.add(bcombatattack);
		bottomPanel.add(bcombatevade);
		bottomPanel.add(bcombatstatus);
		
		refresh(bottomPanel);
		clearLog();
		Game.startCombat();
		
	}// end drawBottomCombat
	
	
	
	public void drawStatus(){
		
		Ship playership = (Game.getPlayer().getShip());
		
		tabStatusPanel.add(new JLabel("Ship Name: " + playership.getName()));
		tabStatusPanel.add(new JLabel("Ship Hull: " + Integer.toString(playership.getCurrentHull()) + "/" + Integer.toString(playership.getHull())));
		tabStatusPanel.add(new JLabel("Ship Weapon Power: " + Integer.toString(playership.getCurrentWeapons())));
		
		refresh(tabStatusPanel);
		
	}//end drawStatus
	
	
	
	public void drawMain(){
		currentwindow = ACTIVE_MAIN;
		
		
		logwindow = new JTextArea();
		logwindow.setEditable(false);
		logscroll = new JScrollPane(logwindow);
		
		
		logscroll.setPreferredSize(LOG_WINDOW_SIZE);
				
		logPanel.add(logscroll);
		
		addText("Main window text");
		
		refresh(logPanel);
		
		drawBottomMain();
	}//end drawMain
	
	

	

	//************************ LISTENERS ***************
	
	public void actionPerformed(ActionEvent e){
		
		//TODO redo all of this
		System.out.println(e.getActionCommand());
		
		
		if (e.getActionCommand().equals("Combat")){
			drawBottomCombat();
			Game.startCombat();
			Game.getCombat().startTurn();
		}//end if
		
		if (e.getActionCommand().equals("Fire Weapons!")){
			Game.getCombat().combatAttack();
		}//end
		
		
		/*
		if (e.getSource() == bstatus){
			if (currentwindow == ACTIVE_STATUS){
				drawMain();
			}//end if
			else{	
			drawStatus();
			}//end else		
		}//end if getSource bstatus
		
		if (e.getSource() == bcombat){
			
		}//endif
		
		
		if (e.getSource() == bcombatattack){
				
		}//end combat button if
		*/
		
		
		//TODO redo action listeners
		
		
		
	}//end actionListener
	
		
	
	
	
	
	
	
	
	
	
        //******************************UTILITY AND CLEANUP **************
	
	//just runs a repaint and revalidate in one call.
	//I don't know if this is a good or bad way to do it.
	private void refresh(JPanel p){
		p.revalidate();
		p.repaint();
	}//end refresh
	
	
	public void addText(String s){
		logwindow.append(s + "\n");
		refresh(logPanel);
	}// end addText
	
	
	private void clearLog(){
		logwindow.setText("");
	}
	
	
	
	//******************************* Setup Methods ****************************//
	
//Don't know if these will stay in this class
//Will probably be removed once I get a start screen	
	
	public static String playerInput(String text){
		String s = "";
		
		s = JOptionPane.showInputDialog(text);
		
		return s;
	}//end player setup
	
	
}//end MainGUI class
